import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import axios from "axios";

//导入持久化vuex数据的插件
import persisted from "vuex-persistedstate";
export default new Vuex.Store({
  //使用插件
  plugins: [persisted()],
  state: {
    token: "",
    user: {},
  },
  getters: {},
  mutations: {
    //存放token
    setToken(state, payload) {
      state.token = payload;
    },
    setUserInfo(state, user) {
      state.user = user;
    },
  },
  actions: {
    //获取用户信息
    async getUserInfo(context) {
      //提交mutations更新
      const { data: res } = await axios.get("/my/userinfo");
      context.commit("setUserInfo", res.data);
    },
  },
  modules: {},
});
